
package cartagows.wsframework.wssecurity.policy.model;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.apache.neethi.PolicyComponent;

import cartagows.wsframework.wssecurity.policy.SP11Constants;
import cartagows.wsframework.wssecurity.policy.SP12Constants;
import cartagows.wsframework.wssecurity.policy.SPConstants;

public class Layout extends AbstractSecurityAssertion {

    private String value = SPConstants.LAYOUT_LAX;
    
    public Layout(int version) {
        setVersion(version);
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        if (SPConstants.LAYOUT_LAX.equals(value)
                || SPConstants.LAYOUT_STRICT.equals(value)
                || SPConstants.LAYOUT_LAX_TIMESTAMP_FIRST.equals(value)
                || SPConstants.LAYOUT_LAX_TIMESTAMP_LAST.equals(value)) {
            this.value = value;
        } else {
            // throw new WSSPolicyException("Incorrect layout value : " +
            // value);
        }
    }

    public QName getName() {
        if ( version == SPConstants.SP_V12 ) {
            return SP12Constants.LAYOUT;
        } else {
            return SP11Constants.LAYOUT; 
        }  
    }

    public PolicyComponent normalize() {
        throw new UnsupportedOperationException();
    }

    public void serialize(XMLStreamWriter writer) throws XMLStreamException {

        String localName = getName().getLocalPart();
        String namespaceURI = getName().getNamespaceURI();

        String prefix = writer.getPrefix(namespaceURI);

        if (prefix == null) {
            prefix = getName().getPrefix();
            writer.setPrefix(prefix, namespaceURI);
        }

        // <sp:Layout>
        writer.writeStartElement(prefix, localName, namespaceURI);

        // <wsp:Policy>
        writer.writeStartElement(SPConstants.POLICY.getPrefix(), SPConstants.POLICY
                .getLocalPart(), SPConstants.POLICY.getNamespaceURI());

        // .. <sp:Strict /> | <sp:Lax /> | <sp:LaxTsFirst /> | <sp:LaxTsLast /> ..
        if (SPConstants.LAYOUT_STRICT.equals(value)) {
            writer.writeStartElement(prefix, SPConstants.LAYOUT_STRICT, namespaceURI);
            
        } else if (SPConstants.LAYOUT_LAX.equals(value)) {
            writer.writeStartElement(prefix, SPConstants.LAYOUT_LAX, namespaceURI);
            
        } else if (SPConstants.LAYOUT_LAX_TIMESTAMP_FIRST.equals(value)) {
            writer.writeStartElement(prefix, SPConstants.LAYOUT_LAX_TIMESTAMP_FIRST, namespaceURI);
            
        } else if (SPConstants.LAYOUT_LAX_TIMESTAMP_LAST.equals(value)) {
            writer.writeStartElement(prefix, SPConstants.LAYOUT_LAX_TIMESTAMP_LAST, namespaceURI);
        }
        
        writer.writeEndElement();
        
        // </wsp:Policy>
        writer.writeEndElement();
        
        // </sp:Layout>
        writer.writeEndElement();
    }
}
